<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\QuestionType;
class Leave extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 年级列表
     */
    public function index()
    {

        $param = $this->request->param();
        $list=model('leave')->where('status',['>',-1])->paginate(config('page_number'));
        $page = input('page')?:0;
        return $this->fetch('index', ['list' =>$list,'page' =>$page]);
    }
    /**
     * 添加年级
     */
    public function add(){
        $param = $this->request->param();
        if ($this->request->isPost()) {
            $param['created_at']=time();
            $param['updated_at']=time();
            if($this->insert('leave', $param) === true){
                insert_admin_log('添加请假');
                $this->success('添加请假成功', url('admin/leave/index'));
            }else{
                $this->error($this->errorMsg);
            }
        }
        $grade=model('grade')->where('status',['>',0])->select();
        $list=model('leave')->where('status',['>',0])->paginate(config('page_number'));
        return $this->fetch('addleave', ['grade' =>$grade,'leave' =>$list]);
    }
    /**
     * 编辑职位
     */
    public function edit()
    {
        $param = $this->request->param();
        if ($this->request->isPost()) {
            $param['updated_at']=time();
            if ($this->update('leave', $param, input('_verify', true)) === true) {
                insert_admin_log('修改了请假');
                $this->success('修改请假成功', url('admin/leave/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        $param['status']=['>',0];
        $grade=model('grade')->where('status',['>',0])->select();
        $list=model('leave')->where('id',$param['id'])->find();
        $list['starting_time']=substr($list['starting_time'],0,10)."T".substr($list['starting_time'],11);
        $list['finish_time']=substr($list['finish_time'],0,10)."T".substr($list['finish_time'],11);
        return $this->fetch('addleave', ['grade' =>$grade,'data' =>$list]);
    }
    /**
     * 删除年级
     */
    public function del()
    {
        if ($this->request->isGet()) {
            $param = $this->request->param();
            if ($this->delete('leave', $param ) === true) {
                insert_admin_log('删除了请假');
                $this->success('请假删除成功', url('admin/leave/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
    }


    /**
     * 状态设置
     */
    public function teacherstatus(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($this->update('leave', $param, input('_verify', true)) === tru){
               $this->success('操作成功！', url('admin/leave/index'));
            } else{
               $this->error($this->errorMsg);
            }
            $this->success('操作成功', url('admin/leave/index'));
        }
    }
    /**
     * 销假设置
     */
    public function report_time(){

        $param = $this->request->param();
        if ($this->request->isGet()) {
            $param['report_time']=date("Y-m-d H:i:s",time());
            $param['updated_at']=date("Y-m-d H:i:s",time());
            if ($this->report('leave', $param, input('_verify', true)) === true) {
                insert_admin_log('销假');
                $this->success('销假成功', url('admin/leave/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
    }


}
